package com.motorhome.h5.service.impl;

import com.motorhome.base.util.DigestUtil;
import com.motorhome.base.util.HttpClientUtil;
import com.motorhome.base.util.JsonUtil;
import com.motorhome.base.util.StringUtil;
import com.motorhome.h5.common.statics.H5Statics;
import com.motorhome.h5.service.OpenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: John
 */
@Service
public class OpenServiceImpl implements OpenService {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public String addMember(String cardId, String password, String trueName, String mobile, String openId) {
        Map<String, String> data = new HashMap<>();
        data.put("cardId", cardId);
        data.put("password", password);
        data.put("trueName", trueName);
        data.put("mobile", mobile);
        data.put("openid", openId);
       return post(H5Statics.ADD_MEMBER_URL, JsonUtil.toJson(data));
    }

    @Override
    public String getMemberInfo(String cardId) {
        Map<String, String> data = new HashMap<>();
        data.put("cardId", cardId);
        return post(H5Statics.GET_MEMBERINFO_URL, JsonUtil.toJson(data));
    }

    @Override
    public String getCoupnSendPagedV2(String cardId) {
        Map<String, String> data = new HashMap<>();
        data.put("userAccount", H5Statics.OPEN_USERACCOUNT);
        data.put("where", " 1=1 and Flag=1 and EnableCount>0 and CardId='" + cardId + "'");
        data.put("pageIndex", "0");
        data.put("pageSize", "200");
        data.put("orderBy", " OperateTime desc");
        return post(H5Statics.GET_COUPNSENDPAGEDV2_URL, JsonUtil.toJson(data));
    }

    @Override
    public String subCoupon(String couponSendGuid, String subCount, String snCodes) {
        Map<String, String> data = new HashMap<>();
        data.put("userAccount", H5Statics.OPEN_USERACCOUNT);
        if (StringUtil.isNotBlank(snCodes)) {
            data.put("snCodes", snCodes);
        } else {
            data.put("couponSendGuid", couponSendGuid);
            data.put("subCount", subCount);
        }
        return post(H5Statics.SUBCOUPON_URL, JsonUtil.toJson(data));
    }

    @Override
    public String consume(String cardId, Float totalMoney, Float totalPaid, Float paidValue,
                          Float paidMoney, Float paidPoint, Float paidCard, Float paidOther) {
        Map<String, Object> data = new HashMap<>();
        data.put("userAccount", H5Statics.OPEN_USERACCOUNT);
        data.put("cardId", cardId);
        data.put("totalMoney", totalMoney);
        data.put("totalPaid", totalPaid);
        data.put("paidValue", paidValue);
        data.put("paidMoney", paidMoney);
        data.put("paidPoint", paidPoint);
        data.put("paidCard", paidCard);
        data.put("paidOther", paidOther);
        return post(H5Statics.CONSUME_URL, JsonUtil.toJson(data));
    }

    private String post(String url, String data) {
        String openId = H5Statics.OPEN_ID;
        String secret = H5Statics.OPEN_SECRET;
        String timestamp = System.currentTimeMillis() + "";
        String signature = DigestUtil.md5(openId + secret + timestamp + data).toUpperCase();

        Map<String, Object> requestParams = new HashMap<>();
        requestParams.put("data", data);

        return new HttpClientUtil().post(
                url + "?openId=" + openId + "&signature=" + signature + "&timestamp=" + timestamp,
                requestParams);
    }

}
